package com.rt.easyjava.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rt.easyjava.common.base.*;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.service.BaseServiceImpl;
import com.rt.easyjava.common.utils.others.DateTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 前端控制器
 * @author wuxiaoyong
 * @since 2021-07-04
 * 用户信息可直接形参获取：String (_userId，_loginName，_userName，_companyId，_userJson) or userDetailsDto.getUserId()
 */
@Slf4j
public class BaseController<S extends BaseServiceImpl, B extends CommonBean, D extends CommonDto> {
   /* @Autowired
    protected MyMapper mapper;

    @Autowired
    protected S baseService;

    protected Result<D> page(D dto, UserDetailsDto userDetailsDto, Class<D> dtoClass, Class<B> beanClass){
        Page<B> page = new Page<>(dto.getPage(), dto.getLimit());
        // 查询条件
        B queryBean = mapper.map(dto, beanClass);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        queryBean.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        queryBean.rebuildDataLimitQueryBean(userDetailsDto);

        // query
        QueryWrapper<B> queryWrapper=new QueryWrapper<>(queryBean);
        //特殊查询条件
        queryWrapper.ge(dto.getCreatedate_Start()!=null,"createdate",dto.getCreatedate_Start());
        queryWrapper.le(dto.getCreatedate_End()!=null,"createdate",dto.getCreatedate_End());
        // queryWrapper.orderBy(tmptTimerTestBizDto.getCondition(), tmptTimerTestBizDto.isAsc(), tmptTimerTestBizDto.getColumn());
        // 支持前端：listQuery.sort='-part_no,-id'
        for (CommonDto sortDto:dto.getSortList()){
            queryWrapper.orderBy(true, sortDto.getAsc(), sortDto.getColumn());
        }

        return baseService.page(page, queryWrapper, dtoClass);
    }

    *//**
        树结构查询方法，非树结构会造一个空方法备用着。
    protected Result tree (Long id) {
        Result<D> result = new Result<>();
        // when id=0 , id=0 is parantId=0 , 默认设置一个空根
        TmptTimerTestBizDto tmptTimerTestBizDto = new TmptTimerTestBizDto();
        tmptTimerTestBizDto.setId(0L);
        if (id!=0) {
            TmptTimerTestBiz tmptTimerTestBiz = tmptTimerTestBizService.getById(id);
            tmptTimerTestBizDto = mapper.map(tmptTimerTestBiz, TmptTimerTestBizDto.class);
        }
        treeReduce(tmptTimerTestBizDto);
        result.setObj(tmptTimerTestBizDto);

        return result.success();
    }

    private void treeReduce(TmptTimerTestBizDto tmptTimerTestBizDto){
    }
     *//*

    protected Result myPage(D dto, UserDetailsDto userDetailsDto, Class<D> dtoClass, Class<B> beanClass){

        Page<B> page = new Page<>(dto.getPage(), dto.getLimit());

        // 查询条件
        B beanQuery = mapper.map(dto, beanClass);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        beanQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        beanQuery.rebuildDataLimitQueryBean(userDetailsDto);

        // 多表查询时，plus自带不支持，这里封装一个工具类处理，mapper.xml中主表别名是t1, 给所有非空字段加别名； 特殊查询条件，在工具类中要忽略掉，最后自定义增加。
        QueryWrapper<B> queryWrapper = MyQueryWrapper.buildQueryWrapper(beanQuery , "createdate");
        //特殊查询条件
        queryWrapper.ge(dto.getCreatedate_Start()!=null,"t1.createdate",dto.getCreatedate_Start());
        queryWrapper.le(dto.getCreatedate_End()!=null,"t1.createdate",dto.getCreatedate_End());
        // queryWrapper.orderBy(tmptTimerTestBizDto.getCondition(), tmptTimerTestBizDto.isAsc(), "t1."+tmptTimerTestBizDto.getColumn());
        // 支持前端：listQuery.sort='-part_no,-id'
        for (CommonDto sortDto:dto.getSortList()){
            queryWrapper.orderBy(true, sortDto.getAsc(), "t1."+sortDto.getColumn());
        }

        return baseService.myPage(page, queryWrapper, dto.getHasFooter(), dtoClass);
    }

    protected Result selectBy(D dto, UserDetailsDto userDetailsDto, Class<D> dtoClass, Class<B> beanClass){
        // 查询条件
        B tmptTimerTestBizQuery = mapper.map(dto, beanClass);
        // set deleteflag default 0, 查询参数时，默认只查有效数据
        tmptTimerTestBizQuery.setDeleteflagDefaultZero();
        // 数据访问边界 0：全部数据， 10：机构内部数据
        tmptTimerTestBizQuery.rebuildDataLimitQueryBean(userDetailsDto);

        QueryWrapper<B> queryWrapper=new QueryWrapper<>(tmptTimerTestBizQuery);
        //特殊查询条件
        queryWrapper.ge(dto.getCreatedate_Start()!=null,"createdate",dto.getCreatedate_Start());
        queryWrapper.le(dto.getCreatedate_End()!=null,"createdate",dto.getCreatedate_End());
        queryWrapper.orderBy(dto.getCondition(), dto.isAsc(), dto.getColumn());

        return  baseService.selectBy(queryWrapper, dtoClass);
    }

    protected Result selectById(Long id, Class<D> dtoClass){
        return baseService.selectById(id, dtoClass);
    }

    *//*protected Result select4column(String columnName){
        return baseService.select4column(columnName);
    }*//*

    protected Result create(D dto, String _userId, String _userName, String _companyId, Class<B> beanClass){
        //插入参数
        dto.setCreatemanId(Long.parseLong(_userId));
        dto.setCreateman(_userName);
        dto.setCreatedate(DateTool.getCurrentDatetimeStr());
        dto.setCompanyId(Long.parseLong(_companyId));

        //调用服务
        return baseService.create(dto, beanClass);
    }

    protected Result update(D dto, String _userId, String _userName, Class<B> beanClass){
        //参数
        dto.setModifymanId(Long.parseLong(_userId));
        dto.setModifyman(_userName);
        dto.setModifydate(DateTool.getCurrentDatetimeStr());
        dto.setCreatedate(null);
        dto.setCreateman(null);
        dto.setCreatemanId(null);
        dto.setCompanyId(null);

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", dto.getId()==null?"0":dto.getId());

        //调用服务
        return baseService.update(dto, updateWrapper, beanClass);
    }

    protected Result delete(Long id, String _userId, String _userName, Class<B> beanClass){
        Result result = new Result();

        //参数
        TmptTimerTestBizDto tmptTimerTestBizDto = new TmptTimerTestBizDto();
        tmptTimerTestBizDto.setModifymanId(Long.parseLong(_userId));
        tmptTimerTestBizDto.setModifyman(_userName);
        tmptTimerTestBizDto.setModifydate(DateTool.getCurrentDatetimeStr());
        tmptTimerTestBizDto.setDeleteflag("1");

        //query condition for update
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id==null?0:id.longValue());

        //调用服务
        return baseService.update(tmptTimerTestBizDto, updateWrapper, beanClass);
    }

    protected Result deletePhysical(Long id){
        Result result = new Result();

        //调用服务
        Boolean aBoolean =  false;//tmptTimerTestBizService.removeById(id==null?0:id.longValue());
        if(aBoolean){
            result.success();
        }else {
            result.fault();
        }

        return result;
    }
*/
}
